package com.richapp.launcher.net.http;

import android.util.Log;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by jack on 2015/4/16 0016.
 */
public class MyHttpClient {

    private static final String TAG = MyHttpClient.class.getName();
    public static String HTTP_CODING = "UTF8";


    public String get(String url,Map<String,Object> paras){
        String result = null;
        try{
            HttpClient httpClient = new DefaultHttpClient();

            StringBuffer urlBuffer = new StringBuffer();
            urlBuffer.append(url);
            Set<String> keySets = paras.keySet();
            int count = keySets.size();
            Iterator<String> iterator = keySets.iterator();
            String key;
            int i = 0;
            while(iterator.hasNext()){
                key = iterator.next();
                urlBuffer.append(URLEncoder.encode(key,HTTP_CODING));
                urlBuffer.append("=");
                urlBuffer.append(URLEncoder.encode(paras.get(key).toString(),HTTP_CODING));
                if(i != count -1){
                    urlBuffer.append("&");
                }
                i++;
            }

            HttpGet httpGet = new HttpGet(urlBuffer.toString());
            HttpResponse response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                InputStream inputStream = response.getEntity().getContent();
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                int length = -1;
                byte[] buffer = new byte[1024];
                while((length = inputStream.read(buffer)) != -1){
                    bos.write(buffer,0,length);
                }
                inputStream.close();
                result = new String(bos.toByteArray(),HTTP_CODING);
            }
        }catch (Exception e){
            Log.e(TAG,"HttpGet出错:"+e.getMessage());
        }
        return result;
    }

    public void post(String url,Map<String,Object> paras){

    }

    public void uploadFile(String url,Map<String,Object> paras){

    }

    public boolean downloadFile(String downloadUrl, String saveDirStr,String storeFileName){
        boolean flag = false;
        try{
            URL url = new URL(downloadUrl);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();

            //判断父目录存在
            File saveDir = new File(saveDirStr);
            if(!saveDir.exists()){
                saveDir.mkdirs();
            }
            //判断是否有相同文件存在
            File stroeFile = new File(saveDir.getAbsolutePath()+File.separator+storeFileName);
            if(stroeFile.exists()){
                stroeFile.delete();
            }
            //新建文件
            stroeFile.createNewFile();
            //读取文件并写入路径
            InputStream inputStream = connection.getInputStream();
            FileOutputStream outputStream = new FileOutputStream(stroeFile);
            byte[] buffer = new byte[1024];
            int length = -1;
            while((length = inputStream.read(buffer)) != -1){
                outputStream.write(buffer,0,length);
            }
            outputStream.flush();
            inputStream.close();
            outputStream.close();
            //下载完成
            flag = true;
        }catch (Exception e){
            Log.e(TAG, "下载文件错误:" + e.getMessage());
        }
        return flag;
    }



}
